clear
clear all
cap log close

log using "log-files/2_Figure_D1.smcl", replace smcl

timer on 2

*------------*
* Figure D.1 *
*------------*
	* Figure D.1.a
	*-------------
		use "data/outputs/computo_verificadortrepdate_missingsatthend.dta", clear

		* Recinto ID
		egen reci_id = group(Pais Dep Prov Muni Loc Reci)
			/* Note, in constructing the data set we made a correction
			   such that recinto names are unique within localities. */

		* Number of mesas
		egen nmesas = count(mshare_nbnn), by(reci_id)

		* Margin
		gen margin_nbnn = mshare_nbnn - cshare_nbnn

		* Cutoff
		quietly sum pcs_nbnn if trep_date_verificador >= tc(20oct2019 19:40:57) & ///
			trep_date_verificador <= tc(20oct2019 19:40:58), detail
		local cutoff = `r(min)' 
		local label = round(`cutoff', .01)

		gen chosen_reci = (Dep == "Potosí" & Muni == "Llallagua" & ///
			Reci == "Col. 1ro. De Mayo")

		lprobust margin_nbnn pcs_nbnn, genvars

		twoway (line lprobust_gx_bc lprobust_eval, ///
				lcolor(gs6) lwidth(medthick)) ///
			(line lprobust_CI_l_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)) ///
			(line lprobust_CI_r_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)) ///
			(scatter margin_nbnn pcs_nbnn if chosen_reci == 1, mcolor(midblue) ///
				msize(small) msymbol(Dh)) /// 
			(lfit margin_nbnn pcs_nbnn if chosen_reci == 1, lcolor(midblue) ///
				lwidth(medthick)), ///
			graphregion(color(white)) ///
			xline(`cutoff', lcolor(black) lpattern(dash)) ///
			ylab(, glcolor(none) angle(0)) ///
			xlab(0 `cutoff' "`label'" 1) ///
			ytitle("MAS Vote Margin") ///
			xtitle("Proportion of Preliminary Vote Counted") ///
			xsize(7) ysize(5) ///
			legend(pos(11) ring(0) rows(3) symxsize(*.4) ///
			order(4 "Voting booths from one" "precinct (example)" ///
				1 "Overall vote margin trend"))
		graph export "outputs/generated/Figure_D1a.pdf", replace

	* Figure D.1.b
	*-------------
		use "data/outputs/computo_verificadortrepdate_missingsatthend.dta", clear	

		* Nulos share
		gen nulosshare = Nulos / total_so

		* Margin
		gen margin_nbnn = mshare_nbnn - cshare_nbnn

		* Recinto ID
		egen reci_id = group(Pais Dep Prov Muni Loc Reci)

		foreach var of varlist mshare_nbnn cshare_nbnn margin_nbnn nulosshare {
			egen reci_mean = mean(`var'), by(reci_id)
			gen `var'_dm = `var' - reci_mean 
			drop reci_mean
			qui su `var'
			gen `var'_dm_plus = `var'_dm + `r(mean)' // Add overall mean for scale
		}
	
		* Cutoff
		quietly sum pcs_nbnn if trep_date_verificador >= tc(20oct2019 19:40:57) & ///
			trep_date_verificador <= tc(20oct2019 19:40:58), detail
		local cutoff = `r(min)' 
		local label = round(`cutoff', .01)

		binsreg margin_nbnn_dm pcs_nbnn, savedata(temp) replace

		gen dots_binid = _n
		merge 1:1 dots_binid using "temp.dta"
		drop _merge
		erase "temp.dta"

		lprobust margin_nbnn_dm pcs_nbnn, genvars
	
		twoway (scatter dots_fit dots_x, msize(small) mcolor(midblue)) ///
			(line lprobust_gx_bc lprobust_eval, ///
				lcolor(gs6) lwidth(medthick)) ///
			(line lprobust_CI_l_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)) ///
			(line lprobust_CI_r_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)), ///
			graphregion(color(white)) ///
			xline(`cutoff', lcolor(black) lpattern(dash)) ///
			ylab(, glcolor(none) angle(0)) ///
			xlab(0 `cutoff' "`label'" 1) ///
			ytitle("MAS Margin Minus Precinct Mean") ///
			xtitle("Proportion of Preliminary Vote Counted") ///
			xsize(7) ysize(5) ///
			legend(off)
		graph export "outputs/generated/Figure_D1b.pdf", replace

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
